package com.flyxiang.workflow.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.flyxiang.common.idempotent.annotation.RepeatSubmit;
import com.flyxiang.common.log.annotation.Log;
import com.flyxiang.common.web.core.BaseController;
import com.flyxiang.common.mybatis.core.page.PageQuery;
import com.flyxiang.common.core.domain.R;
import com.flyxiang.common.core.validate.AddGroup;
import com.flyxiang.common.core.validate.EditGroup;
import com.flyxiang.common.log.enums.BusinessType;
import com.flyxiang.common.excel.utils.ExcelUtil;
import com.flyxiang.workflow.domain.vo.WfDynamicDataVo;
import com.flyxiang.workflow.domain.bo.WfDynamicDataBo;
import com.flyxiang.workflow.service.IWfDynamicDataService;
import com.flyxiang.common.mybatis.core.page.TableDataInfo;

/**
 * 动态表单数据
 *
 * @author daojiang
 * @date 2024-11-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/workflow/dynamicData")
public class WfDynamicDataController extends BaseController {

    private final IWfDynamicDataService wfDynamicDataService;

    /**
     * 查询动态表单数据列表
     */
    @SaCheckPermission("workflow:dynamicData:list")
    @GetMapping("/list")
    public TableDataInfo<WfDynamicDataVo> list(WfDynamicDataBo bo, PageQuery pageQuery) {
        return wfDynamicDataService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出动态表单数据列表
     */
    @SaCheckPermission("workflow:dynamicData:export")
    @Log(title = "动态表单数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WfDynamicDataBo bo, HttpServletResponse response) {
        List<WfDynamicDataVo> list = wfDynamicDataService.queryList(bo);
        ExcelUtil.exportExcel(list, "动态表单数据", WfDynamicDataVo.class, response);
    }

    /**
     * 获取动态表单数据详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("workflow:dynamicData:query")
    @GetMapping("/{id}")
    public R<WfDynamicDataVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(wfDynamicDataService.queryById(id));
    }

    /**
     * 新增动态表单数据
     */
//    @SaCheckPermission("workflow:dynamicData:add")
    @Log(title = "动态表单数据", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<WfDynamicDataVo> add(@Validated(AddGroup.class) @RequestBody WfDynamicDataBo bo) {
        return R.ok(wfDynamicDataService.insertByBo(bo));
    }

    /**
     * 修改动态表单数据
     */
    @SaCheckPermission("workflow:dynamicData:edit")
    @Log(title = "动态表单数据", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WfDynamicDataBo bo) {
        return toAjax(wfDynamicDataService.updateByBo(bo));
    }

    /**
     * 删除动态表单数据
     *
     * @param ids 主键串
     */
    @SaCheckPermission("workflow:dynamicData:remove")
    @Log(title = "动态表单数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(wfDynamicDataService.deleteWithValidByIds(List.of(ids), true));
    }
}
